﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ICSharpCode.AvalonEdit;
using System.IO;
using System.Xml;
using ICSharpCode.AvalonEdit.CodeCompletion;

namespace JavaScriptEditor
{
    public partial class MyScriptEditor : UserControl
    {
        public TextEditor ScriptEditor = new TextEditor();

        public string Script
        {
            get
            {
                return ScriptEditor.Text;
            }
            set
            {
                ScriptEditor.Text = value;
            }
        }

        public MyScriptEditor()
        {
            InitializeComponent();
            InitScriptEditor();
        }

        public void InitScriptEditor()
        {
            MyWpfHost.Child = ScriptEditor;
            ScriptEditor.ShowLineNumbers = true;
            ScriptEditor.FontSize = 18d;
            ScriptEditor.FontFamily = new System.Windows.Media.FontFamily("Consolas");
            string FilePath = Application.StartupPath + "\\Config\\JavaScript.Config";
            if (!File.Exists(FilePath))
            {
                FilePath = Application.StartupPath + "\\Config\\JavaScript_System.Config";
            }
            if (!File.Exists(FilePath))
            {
                string xml = JavaScriptEditor.Properties.Resources.JavaScriptConfig;
                using (Stream XmlFile = new MemoryStream(UTF8Encoding.Default.GetBytes(xml)))
                {
                    using (XmlTextReader XmlReader = new XmlTextReader(XmlFile))
                    {
                        ScriptEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(XmlReader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
                    }
                }
            }
            else
            {
                using (Stream XmlFile = File.OpenRead(FilePath))
                {
                    using (XmlTextReader XmlReader = new XmlTextReader(XmlFile))
                    {
                        ScriptEditor.SyntaxHighlighting = ICSharpCode.AvalonEdit.Highlighting.Xshd.HighlightingLoader.Load(XmlReader, ICSharpCode.AvalonEdit.Highlighting.HighlightingManager.Instance);
                    }
                }
            }
            //ScriptEditor.TextArea.TextEntering += TextArea_TextEntering;
            //ScriptEditor.TextArea.TextEntered += TextArea_TextEntered;
        }

        //void TextArea_TextEntered(object sender, System.Windows.Input.TextCompositionEventArgs e)
        //{
        //    if (e.Text == ".")
        //    {
        //        if (completionWindow == null)
        //        {
        //            completionWindow = new CompletionWindow(ScriptEditor.TextArea);
        //        }
        //        IList<ICompletionData> data = completionWindow.CompletionList.CompletionData;
        //        data.Add(new MyCompletionData("Item1"));
        //        data.Add(new MyCompletionData("Item2"));
        //        data.Add(new MyCompletionData("Item3"));
        //        completionWindow.Show();
        //        completionWindow.Closed += delegate { completionWindow = null; };
        //    }
        //}
        //CompletionWindow completionWindow = null;
        //void TextArea_TextEntering(object sender, System.Windows.Input.TextCompositionEventArgs e)
        //{
        //    if (e.Text.Length > 0 && completionWindow != null)
        //    {
        //        if (!char.IsLetterOrDigit(e.Text[0]))
        //        {
        //            completionWindow.CompletionList.RequestInsertion(e);
        //        }
        //    }
        //}
    }
}
